package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.domain.BaseEntity;
import com.yiyahui.framework.web.page.TableDataInfo;
import com.yiyahui.project.erp.domain.YiyaAuditDiscount;
import com.yiyahui.project.erp.domain.YiyaBillingMain;
import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.YiyaClosingWaterRelation;
import com.yiyahui.project.erp.domain.YiyaItemBilling;
import com.yiyahui.project.erp.domain.YiyaMainClosingEntries;
import com.yiyahui.project.erp.domain.requestVo.YiyaItemBillingExtend;
import com.yiyahui.project.erp.domain.responseVo.AuditDiscountExtend;
import com.yiyahui.project.erp.service.IYiyaAuditDiscountService;
import com.yiyahui.project.erp.service.IYiyaBillingMainService;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import com.yiyahui.project.erp.service.IYiyaClosingWaterRelationService;
import com.yiyahui.project.erp.service.IYiyaItemBillingService;
import com.yiyahui.project.erp.service.IYiyaMainClosingEntriesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 折扣审核Controller
 * 
 * @author yiyahui
 * @date 2020-06-09
 */
@Api("折扣审核")
@RestController
@RequestMapping("/erp/discount")
public class YiyaAuditDiscountController extends BaseController
{
    @Autowired
    private IYiyaAuditDiscountService yiyaAuditDiscountService;

    @Autowired
    private IYiyaBillingMainService yiyaBillingMainService;

    @Autowired
    private IYiyaItemBillingService yiyaItemBillingService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;

    @Autowired
    private IYiyaClosingWaterRelationService yiyaClosingWaterRelationService;

    @Autowired
    private IYiyaMainClosingEntriesService yiyaMainClosingEntriesService;
 
    /**
     * 查询折扣审核列表
     */
    @ApiOperation("查询折扣审核列表")
    @PreAuthorize("@ss.hasPermi('erp:discount:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaAuditDiscount yiyaAuditDiscount)
    {
        startPage();
        List<YiyaAuditDiscount> list = yiyaAuditDiscountService.selectYiyaAuditDiscountList(yiyaAuditDiscount);
        return getDataTable(list);
    }

    /**
     * 导出折扣审核列表
     */
    @ApiOperation("导出折扣审核列表")
    @PreAuthorize("@ss.hasPermi('erp:discount:export')")
    @Log(title = "折扣审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaAuditDiscount yiyaAuditDiscount)
    {
        List<YiyaAuditDiscount> list = yiyaAuditDiscountService.selectYiyaAuditDiscountList(yiyaAuditDiscount);
        ExcelUtil<YiyaAuditDiscount> util = new ExcelUtil<YiyaAuditDiscount>(YiyaAuditDiscount.class);
        return util.exportExcel(list, "discount");
    }

    /**
     * 获取折扣审核详细信息
     */
    @ApiOperation("获取折扣审核详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:discount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaAuditDiscountService.selectYiyaAuditDiscountById(id));
    }

    /**
     * 新增折扣审核
     */
    @ApiOperation("新增折扣审核")
    @ApiImplicitParam(name = "yiyaAuditDiscount", value = "${tableComment}", dataType = "YiyaAuditDiscount")
    @PreAuthorize("@ss.hasPermi('erp:discount:add')")
    @Log(title = "折扣审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaAuditDiscount yiyaAuditDiscount)
    {
        return toAjax(yiyaAuditDiscountService.insertYiyaAuditDiscount(yiyaAuditDiscount));
    }

    /**
     * 修改折扣审核
     */
    @ApiOperation("修改折扣审核")
    @ApiImplicitParam(name = "yiyaAuditDiscount", value = "${tableComment}", dataType = "YiyaAuditDiscount")
    @PreAuthorize("@ss.hasPermi('erp:discount:edit')")
    @Log(title = "折扣审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaAuditDiscount yiyaAuditDiscount)
    {
        return toAjax(yiyaAuditDiscountService.updateYiyaAuditDiscount(yiyaAuditDiscount));
    }

    /**
     * 删除折扣审核
     */
    @ApiOperation("删除折扣审核")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:discount:remove')")
    @Log(title = "折扣审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaAuditDiscountService.deleteYiyaAuditDiscountByIds(ids));
    }


    /**
     * 自定义折扣审核列表
     */
    @ApiOperation("自定义折扣审核列表")
    @PostMapping("/list/extend")
    public TableDataInfo listExtend(@RequestBody YiyaAuditDiscount yiyaAuditDiscount)
    {
        startPageByPost(yiyaAuditDiscount);
        List<YiyaAuditDiscount> list = yiyaAuditDiscountService.selectYiyaAuditDiscountList(yiyaAuditDiscount);
        List<YiyaBillingMain> yiyaBillingMains = yiyaBillingMainService.selectByIds(list.stream().map(YiyaAuditDiscount::getBillingMainId).collect(Collectors.toList()));
        Map<Long, YiyaBillingMain> yiyaBillingMainMap = yiyaBillingMains.stream().collect(Collectors.toMap(YiyaBillingMain::getId, a -> a, (k1, k2) -> k1));
        List<YiyaMainClosingEntries> yiyaMainClosingEntriesList = yiyaMainClosingEntriesService.selectByBillingMainIds(list.stream().map(YiyaAuditDiscount::getBillingMainId).collect(Collectors.toList()));
        YiyaMainClosingEntries yiyaMainClosingEntries = yiyaMainClosingEntriesList.size() > 0 ? yiyaMainClosingEntriesList.get(0) : null;
        List<YiyaItemBillingExtend> yiyaItemBillingExtends = yiyaItemBillingService.selectByBillingMainIds(list.stream().map(YiyaAuditDiscount::getBillingMainId).collect(Collectors.toList()));
        YiyaClosingWaterRelation yiyaClosingWaterRelationRequest = new YiyaClosingWaterRelation();
        yiyaClosingWaterRelationRequest.setMainClosingEntriesId(yiyaMainClosingEntries != null ? yiyaMainClosingEntries.getId() : 0);
        Map<Long, YiyaClosingWaterRelation> closingWaterRelationMap = yiyaClosingWaterRelationService.selectYiyaClosingWaterRelationList(yiyaClosingWaterRelationRequest).stream().collect(Collectors.toMap(YiyaClosingWaterRelation::getItemBillingId, a -> a, (k1, k2) -> k1));
        yiyaItemBillingExtends.forEach(r -> {
            YiyaClosingWaterRelation yiyaClosingWaterRelation = closingWaterRelationMap.get(r.getId());
            r.setStoredDeductPrice(yiyaClosingWaterRelation != null ? yiyaClosingWaterRelation.getStoredDeductPrice() : null);
            r.setThisPay(yiyaClosingWaterRelation != null ? yiyaClosingWaterRelation.getThisPay() : null);
        });
        Map<Long, List<YiyaItemBillingExtend>> itemMap = yiyaItemBillingExtends.stream().collect(Collectors.groupingBy(YiyaItemBilling::getBillingMainId));
        Map<Long, YiyaClinicPerson> yiyaClinicPersonMap = yiyaClinicPersonService.selectByIds(yiyaBillingMains.stream().map(YiyaBillingMain::getClinicPersonId).collect(Collectors.toList())).stream().collect(Collectors.toMap(YiyaClinicPerson::getId, a -> a, (k1, k2) -> k1));
        List<AuditDiscountExtend> auditDiscountExtendResponses = new ArrayList<>();
        list.forEach(r -> {
            YiyaBillingMain yiyaBillingMain = yiyaBillingMainMap.get(r.getBillingMainId());
            AuditDiscountExtend auditDiscountExtendResponse = new AuditDiscountExtend();
            auditDiscountExtendResponse.setYiyaBillingMain(yiyaBillingMain);
            auditDiscountExtendResponse.setYiyaAuditDiscount(r);
            auditDiscountExtendResponse.setYiyaItemBillingExtends(itemMap.get(r.getBillingMainId()));
            auditDiscountExtendResponse.setYiyaClinicPerson(yiyaClinicPersonMap.get(yiyaBillingMain.getClinicPersonId()));
            auditDiscountExtendResponses.add(auditDiscountExtendResponse);
        });
        return getDataTable(auditDiscountExtendResponses,list);
    }
}
